﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmRoleList : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        TextBox tx1, tx2;
        public frmRoleList(TextBox tx1,TextBox tx2)
        {
            InitializeComponent();
            this.tx1 = tx1;
            this.tx2 = tx2;
            
        }

        private void frmRoleList_Load(object sender, EventArgs e)
        {
            LoalAllRole();
        }
        public void LoalAllRole()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "select 使用者權限代號,權限名稱,備註 from 使用者權限";
                SqlDataAdapter da = new SqlDataAdapter(strcmd, con);
                //da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView權限清單.DataSource = dt;
                PublicClass.setRowNumber(dataGridView權限清單);
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn查詢_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string strcmd = "sp_權限清單";
                SqlDataAdapter da = new SqlDataAdapter(strcmd, con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (txt權限代號或名稱.Text!=null) da.SelectCommand.Parameters.AddWithValue("@權限代號或名稱", txt權限代號或名稱.Text);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView權限清單.DataSource = dt;
                PublicClass.setRowNumber(dataGridView權限清單);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                int r = dataGridView權限清單.CurrentCell.RowIndex;
                //int r = e.RowIndex;
                //MessageBox.Show(dataGridView權限清單.Rows[r].Cells[0].Value.ToString());
                tx1.Text = dataGridView權限清單.Rows[r].Cells[0].Value.ToString();
                tx2.Text = dataGridView權限清單.Rows[r].Cells[1].Value.ToString();
                this.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
    }
}
